const { RouterService } = require('../../services/admin');
const RouterController = {
  // 添加
  add: async (req, res) => {
    try {
      const { title, path, type = 0, icon, meta = {}, parentId, component = '', auth = [], menuType = 0 } = req.body;
      const result = await RouterService.add({ title, path, type, icon, meta, component, auth, menuType, parentId: parentId ? parentId : null });
      if (result.success) {
        res.send({
          code: 1,
          ...result,
        });
      } else {
        res.send({
          code: 0,
          ...result,
        });
      }
    } catch (error) {
      res.send({
        success: false,
        code: 0,
        msg: '添加失败' + error,
      });
    }
  },
  //编辑
  edit: async (req, res) => {
    try {
      const { _id, title, path, type = 0, icon, meta = {}, parentId, component = '', menuType = 0, auth = [] } = req.body;
      const result = await RouterService.edit(_id, { title, path, type, icon, meta, component, menuType, auth, parentId: parentId ? parentId : null });

      if (result.success) {
        res.send({
          code: 1,
          ...result,
        });
      } else {
        res.send({
          code: 0,
          ...result,
        });
      }
    } catch (error) {
      res.send({
        success: false,
        code: 0,
        msg: '编辑失败' + error,
      });
    }
  },
  // 列表
  list: async (req, res) => {
    try {
      const result = await RouterService.list(req.body);
      if (result.success) {
        res.send({
          code: 1,
          ...result,
        });
      } else {
        res.send({
          code: 0,
          ...result,
        });
      }
    } catch (error) {
      res.send({
        success: false,
        code: 0,
        msg: '查询失败' + error,
      });
    }
  },
  // 删除
  delete: async (req, res) => {
    try {
      const { _id } = req.body;
      if (!_id) {
        res.send({
          success: false,
          code: 0,
          msg: '参数错误',
        });
        return;
      }
      const result = await RouterService.delete(_id);
      if (result.success) {
        res.send({
          code: 1,
          ...result,
        });
      } else {
        res.send({
          code: 0,
          ...result,
        });
      }
    } catch (error) {
      res.send({
        success: false,
        code: 0,
        msg: '删除失败' + error,
      });
    }
  },
  // 获取详情
  detail: async (req, res) => {
    try {
      const { _id } = req.body;
      if (!_id) {
        res.send({
          success: false,
          code: 0,
          msg: '参数错误',
        });
        return;
      }

      const result = await RouterService.findById(_id);
      if (result.success) {
        res.send({
          code: 1,
          ...result,
        });
      } else {
        res.send({
          code: 0,
          ...result,
        });
      }
    } catch (error) {
      res.send({
        success: false,
        code: 0,
        msg: '查询失败' + error,
      });
    }
  },

  // 获取所的菜单节点
  getAllMenu: async (req, res) => {
    try {
      const result = await RouterService.getAllMenu();
      if (result.success) {
        res.send({
          code: 1,
          ...result,
        });
      } else {
        res.send({
          code: 0,
          ...result,
        });
      }
    } catch (error) {
      res.send();
    }
  },
};

module.exports = RouterController;
